package mshop.com.schild_kotlin.utils

import android.graphics.Rect
import android.support.v7.widget.RecyclerView
import android.view.View

/**
 * Created by mayi on 2018/5/27.
 * @Autor CaiWF
 * @Email 401885064@qq.com
 * @TODO
 */
class ItemGridLayoutDecoration(var space: Int, var count: Int) : RecyclerView.ItemDecoration() {


    override fun getItemOffsets(outRect: Rect?, view: View?, parent: RecyclerView?, state: RecyclerView.State?) {
        //不是第一个的格子都设一个左边和底部的间距
        outRect?.left = space / 2
        outRect?.right = space / 2
        outRect?.bottom = space

        //由于每行都只有count个，所以第一个都是count的倍数，把左边距设为0
        if (parent?.getChildLayoutPosition(view)!! % count == 0) {
            outRect?.left = 0
        } else if (parent.getChildAdapterPosition(view) % count == (count - 1)) {
            outRect?.right = 0
        }

    }
}